package com.jy.api.service.impl;

import com.jy.api.dao.MemberAccountDao;
import com.jy.api.dao.MemberDao;
import com.jy.api.dao.MemberTeamAccountDao;
import com.jy.api.dao.TeamTransactionDao;
import com.jy.api.domain.*;
import com.jy.api.service.TeamTransactionService;
import com.jy.api.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by xiaohua on 2018/12/24.
 */
@Service
public class TeamTransactionServiceImpl implements TeamTransactionService {

    @Autowired
    private TeamTransactionDao teamTransactionDao;
    @Autowired
    private MemberAccountDao accountDao;
    @Autowired
    private MemberTeamAccountDao teamAccountDao;
    @Autowired
    private MemberDao memberDao;

    @Override
    @Transactional
    public Result addStoreTeams(AddStoreTeamsReq req) {
        List<String> strings = new ArrayList<>();
        List<TeamTransaction> transactions = new ArrayList<>();
        for (AddStoreTeamsReq.StoreTeams teams : req.getStoreTeams()) {
            strings.add(teams.getStoreId());
        }
       /* //这个是查询所有连锁下的会员账号
        List<MemberAccount> memberAccounts = accountDao.findByStoreIdInAndEnabledAndUse(strings, true, false);
        //查询新建的memberTeam
        List<MemberTeamAccount> memberTeamAccounts = teamAccountDao.findByTeamIdAndEnabled(req.getId(), true);*/
        List<TeamTransaction> teamTransaction = teamTransactionDao.findByStoreIdInAndEnable(strings, true);
        if (teamTransaction.size() == 0) {
            for (AddStoreTeamsReq.StoreTeams teams : req.getStoreTeams()) {
                TeamTransaction team = new TeamTransaction(teams.getStoreId(), req.getId(), teams.getName());
                transactions.add(team);
            }
            teamTransactionDao.save(transactions);
            return new Result(200, "连锁店添加成功");
        } else {
            return new Result(400, teamTransaction.get(0).getStoreName() + "等连锁店铺已经存在，请核实");
        }

    }

    @Override
    public List<TeamTransaction> findByTeamsId(int id) {
        List<TeamTransaction> teamTransaction = teamTransactionDao.findByTeamIdAndEnable(id, true);
        return teamTransaction;
    }

    @Override
    @Transactional
    public Result deleteByTeamTranId(int id) {
        TeamTransaction teamTransaction = teamTransactionDao.findOne(id);
        teamTransaction.setEnable(false);
        teamTransactionDao.save(teamTransaction);
        List<MemberAccount> memberAccount = accountDao.findByStoreId(teamTransaction.getStoreId());
        List<MemberAccount> memberAccounts = new ArrayList<>();
        for (MemberAccount account : memberAccount) {
            account.setUse(false);
            account.setEnabled(true);
            memberAccounts.add(account);
        }
        accountDao.save(memberAccounts);
        return new Result(200, "已成功移除" + teamTransaction.getStoreName());
    }
}
